/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tlf.dyjc5.servlet.system;

import com.google.gson.Gson;
import com.tlf.dyjc5.bo.MenuBean;
import com.tlf.dyjc5.service.MenuService;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 *
 * @author tengyu
 */
public class RoleMenuTreeServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
            MenuService menuService = (MenuService) context.getBean("menuService");
            String supercode = request.getParameter("id");
            if (supercode == null) {
                //supercode = (String) request.getSession().getAttribute("menuCode");
                supercode = "01";
            }
            long roleId = Long.parseLong(request.getParameter("roleId"));
            //String jsonText=menuService.createMenuTreeJson(roleId);
            List<Map<String, Object>> list = menuService.getMenuTreeJsonObject(roleId);
            Gson json = new Gson();
            
            String jsonText = json.toJson(list);
            //String jsonText= "[{\"id\":\"0103\",\"menuCode\":\"0103\",\"text\":\"系统管理\",\"children\":[{\"id\":\"010301\",\"menuCode\":\"010301\",\"text\":\"单位管理\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"010302\",\"menuCode\":\"010302\",\"text\":\"用户管理\",\"leaf\":true,\"checked\":false,\"iconCls\":\"icon-user\"},{\"id\":\"010303\",\"menuCode\":\"010303\",\"text\":\"角色管理\",\"leaf\":true,\"checked\":false,\"iconCls\":null},{\"id\":\"010304\",\"menuCode\":\"010304\",\"text\":\"菜单管理\",\"leaf\":true,\"checked\":false,\"iconCls\":\"icon-menu\"},{\"id\":\"010306\",\"menuCode\":\"010306\",\"text\":\"公告管理\",\"leaf\":true,\"checked\":false,\"iconCls\":\"icon-notice\"},{\"id\":\"010307\",\"menuCode\":\"010307\",\"text\":\"日志管理\",\"leaf\":true,\"checked\":false,\"iconCls\":\"icon-log\"},{\"id\":\"010308\",\"menuCode\":\"010308\",\"text\":\"系统参数\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"}],\"checked\":false,\"iconCls\":\"icon-wrench_orange\"},{\"id\":\"0107\",\"menuCode\":\"0107\",\"text\":\"监测点管理\",\"children\":[{\"id\":\"010701\",\"menuCode\":\"010701\",\"text\":\"美好的一天\",\"children\":[],\"checked\":false,\"iconCls\":\"\"},{\"id\":\"010704\",\"menuCode\":\"010704\",\"text\":\"美好的一天\",\"children\":[],\"checked\":false,\"iconCls\":\"\"},{\"id\":\"010708\",\"menuCode\":\"010708\",\"text\":\"装置基本管理\",\"children\":[{\"id\":\"01070801\",\"menuCode\":\"01070801\",\"text\":\"电压等级管理\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01070803\",\"menuCode\":\"01070803\",\"text\":\"监测点类型\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01070802\",\"menuCode\":\"01070802\",\"text\":\"电网类型\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01070804\",\"menuCode\":\"01070804\",\"text\":\"装置厂家\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01070805\",\"menuCode\":\"01070805\",\"text\":\"装置型号管理\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01070806\",\"menuCode\":\"01070806\",\"text\":\"规约管理\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"}],\"checked\":false,\"iconCls\":\"\"},{\"id\":\"010702\",\"menuCode\":\"010702\",\"text\":\"装置台帐管理\",\"children\":[{\"id\":\"01070201\",\"menuCode\":\"01070201\",\"text\":\"装置管理\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01070202\",\"menuCode\":\"01070202\",\"text\":\"监测点管理\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"}],\"checked\":false,\"iconCls\":\"\"},{\"id\":\"010709\",\"menuCode\":\"010709\",\"text\":\"装置运行管理\",\"children\":[{\"id\":\"01070901\",\"menuCode\":\"01070901\",\"text\":\"参数管理\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01070902\",\"menuCode\":\"01070902\",\"text\":\"工况管理\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01070903\",\"menuCode\":\"01070903\",\"text\":\"运行管理\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"}],\"checked\":false,\"iconCls\":\"\"},{\"id\":\"010710\",\"menuCode\":\"010710\",\"text\":\"装置校验管理\",\"children\":[{\"id\":\"01071001\",\"menuCode\":\"01071001\",\"text\":\"校验管理\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01071002\",\"menuCode\":\"01071002\",\"text\":\"校验历史查询\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"}],\"checked\":false,\"iconCls\":\"\"},{\"id\":\"010703\",\"menuCode\":\"010703\",\"text\":\"装置上报变更管理\",\"children\":[{\"id\":\"01070301\",\"menuCode\":\"01070301\",\"text\":\"装置上报管理\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01070302\",\"menuCode\":\"01070302\",\"text\":\"装置变更管理\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"}],\"checked\":false,\"iconCls\":\"\"}],\"checked\":false,\"iconCls\":\"icon-eye\"},{\"id\":\"0104\",\"menuCode\":\"0104\",\"text\":\"电压数据管理\",\"children\":[{\"id\":\"010408\",\"menuCode\":\"010408\",\"text\":\"美好\",\"children\":[],\"checked\":false,\"iconCls\":\"\"},{\"id\":\"010402\",\"menuCode\":\"010402\",\"text\":\"监测点数据\",\"children\":[{\"id\":\"01040203\",\"menuCode\":\"01040203\",\"text\":\"5分钟数据\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01040201\",\"menuCode\":\"01040201\",\"text\":\"监测点日数据\",\"leaf\":true,\"checked\":false,\"iconCls\":\"icon-daydata\"},{\"id\":\"01040202\",\"menuCode\":\"01040202\",\"text\":\"监测点月数据\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01040204\",\"menuCode\":\"01040204\",\"text\":\"监测点季数据\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01040205\",\"menuCode\":\"01040205\",\"text\":\"监测点年数据\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"}],\"checked\":false,\"iconCls\":\"\"},{\"id\":\"010403\",\"menuCode\":\"010403\",\"text\":\"单位合格率数据\",\"children\":[{\"id\":\"01040301\",\"menuCode\":\"01040301\",\"text\":\"日数据\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01040302\",\"menuCode\":\"01040302\",\"text\":\"月数据\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01040303\",\"menuCode\":\"01040303\",\"text\":\"季数据\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01040304\",\"menuCode\":\"01040304\",\"text\":\"年数据\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"}],\"checked\":false,\"iconCls\":\"\"},{\"id\":\"010401\",\"menuCode\":\"010401\",\"text\":\"关联点数据分析\",\"children\":[{\"id\":\"01040101\",\"menuCode\":\"01040101\",\"text\":\"关联点数据分析\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"}],\"checked\":false,\"iconCls\":\"\"},{\"id\":\"010404\",\"menuCode\":\"010404\",\"text\":\"数据锁定old\",\"children\":[{\"id\":\"01040406\",\"menuCode\":\"01040406\",\"text\":\"单位日数据\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01040405\",\"menuCode\":\"01040405\",\"text\":\"监测点日数据\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01040401\",\"menuCode\":\"01040401\",\"text\":\"单位月数据\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01040402\",\"menuCode\":\"01040402\",\"text\":\"监测点月数据\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01040403\",\"menuCode\":\"01040403\",\"text\":\"日数据考勤表\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01040404\",\"menuCode\":\"01040404\",\"text\":\"月数据考勤表\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"}],\"checked\":false,\"iconCls\":\"\"},{\"id\":\"010405\",\"menuCode\":\"010405\",\"text\":\"数据补采Old\",\"children\":[{\"id\":\"01040501\",\"menuCode\":\"01040501\",\"text\":\"电压采集数据完整性查询\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01040502\",\"menuCode\":\"01040502\",\"text\":\"补采5分钟数据\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01040503\",\"menuCode\":\"01040503\",\"text\":\"补采日数据\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01040504\",\"menuCode\":\"01040504\",\"text\":\"补采月数据\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"}],\"checked\":false,\"iconCls\":\"\"},{\"id\":\"010406\",\"menuCode\":\"010406\",\"text\":\"数据补采\",\"children\":[{\"id\":\"01040602\",\"menuCode\":\"01040602\",\"text\":\"补采日数据\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01040601\",\"menuCode\":\"01040601\",\"text\":\"补采月数据\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01040603\",\"menuCode\":\"01040603\",\"text\":\"日考勤表\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01040604\",\"menuCode\":\"01040604\",\"text\":\"月考勤表\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"}],\"checked\":false,\"iconCls\":\"\"},{\"id\":\"010407\",\"menuCode\":\"010407\",\"text\":\"锁定数据维护\",\"children\":[{\"id\":\"01040701\",\"menuCode\":\"01040701\",\"text\":\"日数据维护\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01040702\",\"menuCode\":\"01040702\",\"text\":\"月数据维护\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01040704\",\"menuCode\":\"01040704\",\"text\":\"单位日数据\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01040703\",\"menuCode\":\"01040703\",\"text\":\"单位月数据\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"}],\"checked\":false,\"iconCls\":\"\"}],\"checked\":false,\"iconCls\":\"icon-voltage\"},{\"id\":\"0101\",\"menuCode\":\"0101\",\"text\":\"报表管理\",\"children\":[{\"id\":\"010105\",\"menuCode\":\"010105\",\"text\":\"上报报表\",\"children\":[{\"id\":\"01010501\",\"menuCode\":\"01010501\",\"text\":\"监测点台帐汇总表\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01010502\",\"menuCode\":\"01010502\",\"text\":\"监测点台帐明细表\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01010503\",\"menuCode\":\"01010503\",\"text\":\"5分钟电压统计表\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01010504\",\"menuCode\":\"01010504\",\"text\":\"合格率日统计报表\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01010505\",\"menuCode\":\"01010505\",\"text\":\"合格率月统计报表\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01010506\",\"menuCode\":\"01010506\",\"text\":\"合格率季度统计表\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01010509\",\"menuCode\":\"01010509\",\"text\":\"合格率年度统计报表\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01010507\",\"menuCode\":\"01010507\",\"text\":\"综合电压合格率日报表\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01010508\",\"menuCode\":\"01010508\",\"text\":\"综合电压合格率月报表\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01010510\",\"menuCode\":\"01010510\",\"text\":\"综合电压合格率季度报表\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01010511\",\"menuCode\":\"01010511\",\"text\":\"综合电压合格率年度报表\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01010512\",\"menuCode\":\"01010512\",\"text\":\"实测率报表\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01010513\",\"menuCode\":\"01010513\",\"text\":\"覆盖率报表\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"}],\"checked\":false,\"iconCls\":\"\"},{\"id\":\"010106\",\"menuCode\":\"010106\",\"text\":\"全部报表\",\"children\":[{\"id\":\"01010601\",\"menuCode\":\"01010601\",\"text\":\"监测点台帐汇总表\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01010602\",\"menuCode\":\"01010602\",\"text\":\"监测点台帐明细表\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01010603\",\"menuCode\":\"01010603\",\"text\":\"5分钟电压统计表\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01010604\",\"menuCode\":\"01010604\",\"text\":\"合格率日统计报表\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01010605\",\"menuCode\":\"01010605\",\"text\":\"合格率月统计报表\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01010606\",\"menuCode\":\"01010606\",\"text\":\"合格率季度统计报表\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01010609\",\"menuCode\":\"01010609\",\"text\":\"合格率年度统计报表\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01010607\",\"menuCode\":\"01010607\",\"text\":\"综合电压合格率日报表\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01010608\",\"menuCode\":\"01010608\",\"text\":\"综合电压合格率月报表\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01010610\",\"menuCode\":\"01010610\",\"text\":\"综合电压合格率季度报表\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01010611\",\"menuCode\":\"01010611\",\"text\":\"综合电压合格率年度报表\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01010612\",\"menuCode\":\"01010612\",\"text\":\"实测率报表\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01010613\",\"menuCode\":\"01010613\",\"text\":\"覆盖率报表\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"}],\"checked\":false,\"iconCls\":\"\"}],\"checked\":false,\"iconCls\":\"icon-report\"},{\"id\":\"0106\",\"menuCode\":\"0106\",\"text\":\"升级维护测试\",\"children\":[{\"id\":\"010601\",\"menuCode\":\"010601\",\"text\":\"装置升级\",\"children\":[],\"checked\":false,\"iconCls\":\"\"},{\"id\":\"010602\",\"menuCode\":\"010602\",\"text\":\"系统升级\",\"children\":[],\"checked\":false,\"iconCls\":\"\"},{\"id\":\"010603\",\"menuCode\":\"010603\",\"text\":\"系统数据维护\",\"children\":[{\"id\":\"01060301\",\"menuCode\":\"01060301\",\"text\":\"日数据维护\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01060302\",\"menuCode\":\"01060302\",\"text\":\"月数据维护\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"}],\"checked\":false,\"iconCls\":\"\"},{\"id\":\"010604\",\"menuCode\":\"010604\",\"text\":\"版本\",\"children\":[{\"id\":\"01060401\",\"menuCode\":\"01060401\",\"text\":\"版本号\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01060402\",\"menuCode\":\"01060402\",\"text\":\"操作说明书\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"}],\"checked\":false,\"iconCls\":\"\"},{\"id\":\"010605\",\"menuCode\":\"010605\",\"text\":\"原始数据查看\",\"children\":[],\"checked\":false,\"iconCls\":\"\"},{\"id\":\"010607\",\"menuCode\":\"010607\",\"text\":\"原始数据维护\",\"children\":[{\"id\":\"01060703\",\"menuCode\":\"01060703\",\"text\":\"监测点5分钟数据\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01060701\",\"menuCode\":\"01060701\",\"text\":\"监测点日数据\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01060702\",\"menuCode\":\"01060702\",\"text\":\"监测点月数据\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"}],\"checked\":false,\"iconCls\":\"\"},{\"id\":\"010606\",\"menuCode\":\"010606\",\"text\":\"历史数据维护\",\"children\":[{\"id\":\"01060601\",\"menuCode\":\"01060601\",\"text\":\"单位管理\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01060602\",\"menuCode\":\"01060602\",\"text\":\"监测点管理\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"},{\"id\":\"01060603\",\"menuCode\":\"01060603\",\"text\":\"装置管理\",\"leaf\":true,\"checked\":false,\"iconCls\":\"\"}],\"checked\":false,\"iconCls\":\"\"}],\"checked\":false,\"iconCls\":\"icon-maintenance\"}]";
            out.print(jsonText);
            //out.print("[{text:'abc',width:200,menu:{items:[{text:'s2',menu:{items:[{text:'sss1',href:'../abc.jsp',hrefTarget:'Center'},{text:'sssss2'}]}},{text:'s1'}]}},{text:'bcd'},{text:'cde'}]");
            
            //out.println("[{text:'abc',children:{text:'aaa',leaf:true}}]");
            out.flush();
        } finally {
            out.close();
        }
    }
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
